#__*__coding:utf-8 __*__
#python3

''' auto pp add user
power by ken.kuang

2018. All rights reserved

detail refer to LICENSE
'''
import json
from Crypto.Hash import MD5
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
is_mac = False
is_win = False
import platform
operating_system = platform.system().lower()
if operating_system == 'darwin':
    is_mac = True
elif operating_system == 'windows':
    is_win = True

def path_file(p):
    if is_win :
        return p.replace('/', '\\')
    else :
        return p

def md5_enc(enc):
    m = MD5.new()
    m.update(enc)
    return m.hexdigest()

def md5_str(s):
    return md5_enc(s.encode())

def adduser(id_s, date_str, path):
    md5_id = (md5_str(id_s))
    prvkey_str = (open(path_file('%s/user.key' %(path))).read())
    index = json.load(open('index'))
    index[md5_id] = [date_str, md5_str(prvkey_str), True]
    open('index', 'w').write(json.dumps(index, indent = 1))

if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser(description='修改用户证书信息')
    parser.add_argument('-u', '--uid',
                        default=None,
                        dest='userid',
                        help='客户ID号')
    parser.add_argument('-d', '--date',
                        default=None,
                        dest='date',
                        help='指定日期')
    parser.add_argument('-p', '--path',
                        default=None,
                        dest='path',
                        help='指定用户证书的路径')
    args = vars(parser.parse_args())
    adduser(args['userid'], args['date'], args['path'])
